{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "442bd0b9",
   "metadata": {},
   "source": [
    "# week9 作业：\n",
    ">1.请尝试使用和学习字典的方法  \n",
    ">2.请给一组数据，利用字典查询的方法来查找指定的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d4c0575b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典是另一种可变容器模型，且可存储任意类型对象。\n",
    "# 字典的每个键值 key=>value 对用冒号 : 分割，每个对之间用逗号(,)分割，整个字典包括在花括号 {} 中 ,格式如下所示：\n",
    "#d = {key1 : value1, key2 : value2, key3 : value3 }请尝试使用和学习字典的方法\n",
    "# 键必须是唯一的，但值则不必。\n",
    "# 值可以取任何数据类型，但键必须是不可变的，如字符串，数字。\n",
    "# 一个简单的字典实例：\n",
    "#dict = {'name': 'tululu', 'likes': '阿巴阿巴', 'url': 'www.python.org'}\n",
    "#也可如此创建字典：\n",
    "# dict1 = { 'abc': 287 }\n",
    "# dict2 = { 'abc': 233, 99: 1099 }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f3511cd6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Porsche', 'model': '911', 'year': 1963}\n"
     ]
    }
   ],
   "source": [
    "# 也可使用 dict() 构造函数创建新的字典：\n",
    "thisdict = dict(brand=\"Porsche\", model=\"911\", year=1963)\n",
    "# 请注意，关键字不是字符串字面量\n",
    "# 请注意，使用了等号而不是冒号来赋值\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "16667eda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "911\n"
     ]
    }
   ],
   "source": [
    "# 请尝试使用和学习字典的方法\n",
    "#1.访问字典里的值\n",
    "#A.可以通过在方括号内引用其键名来访问字典的项目：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "x = thisdict[\"model\"]\n",
    "print(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "cf1cf019",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Porsche\n"
     ]
    }
   ],
   "source": [
    "#B.有一个名为 get() 的方法会给你相同的结果：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "x = thisdict.get(\"brand\")\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9e50d45c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Porsche', 'model': 119, 'year': 2021}\n"
     ]
    }
   ],
   "source": [
    "# 2.更改值\n",
    "# 您可以通过引用其键名来更改特定项的值：\n",
    "# 把 \"year\" 改为 2021：\n",
    "# 把\"model\" 改为119：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "thisdict[\"year\"] = 2021\n",
    "thisdict[\"model\"] = 119\n",
    "print(thisdict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d33f4eef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "brand\n",
      "model\n",
      "year\n"
     ]
    }
   ],
   "source": [
    "# 3.遍历字典——使用 for 循环遍历字典。\n",
    "# 循环遍历字典时，返回值是字典的键，但也有返回值的方法。\n",
    "# 实例\n",
    "# 逐个打印字典中的所有键名：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "for key in thisdict:\n",
    "  print(key)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "ba948e20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "White\n"
     ]
    }
   ],
   "source": [
    "# setdefault() 方法使用指定的键返回项目的值。\n",
    "# 如果键不存在，则插入这个具有指定值的键。请看下面的例子。\n",
    "thisdict = {\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "x = thisdict.setdefault(\"color\", \"White\")\n",
    "# 获取 \"color\" 项的值，如果 \"color\" 项不存在，则插入值为 \"white\" 的 \"color\"：\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "b208c7dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['brand', 'model', 'year'])\n"
     ]
    }
   ],
   "source": [
    "# keys() 方法返回 view 对象。返回包含字典键的列表。这个视图对象包含列表形式的字典键。\n",
    "# 该视图对象会反映字典的任何改变，请看下面的例子。\n",
    "# 返回键：\n",
    "\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "\n",
    "x = thisdict.keys()\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c22043c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Porsche\n",
      "911\n",
      "1963\n"
     ]
    }
   ],
   "source": [
    "# 实例\n",
    "# 逐个打印字典中的所有值：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "for x in thisdict:\n",
    "  print(thisdict[x])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2b00a099",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Porsche\n",
      "911\n",
      "1963\n"
     ]
    }
   ],
   "source": [
    "#使用 .values() 函数返回字典的值：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "for x in thisdict.values():\n",
    "  print(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f4346ba7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'key1': 0, 'key2': 0, 'key3': 0}\n"
     ]
    }
   ],
   "source": [
    "# fromkeys() 方法返回具有指定键和值的字典。\n",
    "x = ('key1', 'key2', 'key3')\n",
    "y = 0\n",
    "\n",
    "thisdict = dict.fromkeys(x, y)\n",
    "\n",
    "print(thisdict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8bc8b937",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "brand Porsche\n",
      "model 911\n",
      "year 1963\n"
     ]
    }
   ],
   "source": [
    "#通过使用 items() 函数遍历键和值：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "for x, y in thisdict.items():\n",
    "  print(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4dbb108a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yes, 'model' is one of the keys in the thisdict dictionary\n"
     ]
    }
   ],
   "source": [
    "# 检查键是否存在\n",
    "# 要确定字典中是否存在指定的键，请使用 in 关键字：\n",
    "# 实例\n",
    "# 检查字典中是否存在 \"model\"：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "if \"model\" in thisdict:\n",
    "  print(\"Yes, 'model' is one of the keys in the thisdict dictionary\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "49deeeaa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "# 字典长度\n",
    "# 要确定字典有多少项目（键值对），请使用 len() 方法。\n",
    "# 实例\n",
    "# 打印字典中的项目数：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "\n",
    "print(len(thisdict))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8d894736",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Porsche', 'model': '911', 'year': 1963, 'color': 'red'}\n"
     ]
    }
   ],
   "source": [
    "# 添加项目\n",
    "# 通过使用新的索引键并为其赋值，可以将项目添加到字典中：\n",
    "# 实例\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "thisdict[\"color\"] = \"red\"\n",
    "print(thisdict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "50333f97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Porsche', 'year': 1963}\n"
     ]
    }
   ],
   "source": [
    "# 删除项目\n",
    "# 有几种方法可以从字典中删除项目：\n",
    "# pop() 方法删除具有指定键名的项：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "thisdict.pop(\"model\")\n",
    "print(thisdict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f7c1e7a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Porsche', 'model': '911'}\n"
     ]
    }
   ],
   "source": [
    "# popitem() 方法删除最后插入的项目\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "thisdict.popitem()\n",
    "print(thisdict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d0c99ddf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Porsche', 'year': 1963}\n"
     ]
    }
   ],
   "source": [
    "# del 关键字删除具有指定键名的项目：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "del thisdict[\"model\"]\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "c2c4fa17",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'thisdict' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-29-330f6c731e3f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mthisdict\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mthisdict\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#this 会导致错误，因为 \"thisdict\" 不再存在。\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'thisdict' is not defined"
     ]
    }
   ],
   "source": [
    "# del 关键字也可以完全删除字典：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "del thisdict\n",
    "\n",
    "print(thisdict) #this 会导致错误，因为 \"thisdict\" 不再存在。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d5ac0d3a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "# clear() 关键字清空字典：\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "thisdict.clear()\n",
    "print(thisdict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f3c138fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Porsche', 'model': '911', 'year': 1963}\n"
     ]
    }
   ],
   "source": [
    "# 复制字典\n",
    "# 不能通过键入 dict2 = dict1 来复制字典，因为：dict2 只是对 dict1 的引用，而 dict1 中的更改也将自动在 dict2 中进行。\n",
    "# 有一些方法可以进行复制，一种方法是使用内建的字典方法 copy()。\n",
    "# 使用 copy() 方法来复制字典：\n",
    "\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "mydict = thisdict.copy()\n",
    "print(mydict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "cb8b86a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Porsche', 'model': '911', 'year': 1963}\n"
     ]
    }
   ],
   "source": [
    "# 制作副本的另一种方法是使用内建方法 dict()\n",
    "# 使用 dict() 方法创建字典的副本：\n",
    "\n",
    "thisdict ={\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "mydict = dict(thisdict)\n",
    "print(mydict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a1156cbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'child1': {'name': 'Phoebe Adele', 'year': 2002}, 'child2': {'name': 'Jennifer Katharine', 'year': 1996}, 'child3': {'name': 'Rory John', 'year': 1999}}\n"
     ]
    }
   ],
   "source": [
    "# 嵌套字典\n",
    "# 词典也可以包含许多词典，这被称为嵌套词典。\n",
    "# 实例\n",
    "# 创建包含三个字典的字典：\n",
    "\n",
    "myfamily = {\n",
    "  \"child1\" : {\n",
    "    \"name\" : \"Phoebe Adele\",\n",
    "    \"year\" : 2002\n",
    "  },\n",
    "  \"child2\" : {\n",
    "    \"name\" : \"Jennifer Katharine\",\n",
    "    \"year\" : 1996\n",
    "  },\n",
    "  \"child3\" : {\n",
    "    \"name\" : \"Rory John\",\n",
    "    \"year\" : 1999\n",
    "  }\n",
    "}\n",
    "print(myfamily)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "5a1e5ac5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'child1': {'name': 'Phoebe Adele', 'year': 2002}, 'child2': {'name': 'Jennifer Katharine', 'year': 1996}, 'child3': {'name': 'Rory John', 'year': 1999}}\n"
     ]
    }
   ],
   "source": [
    "# 或者嵌套三个已经作为字典存在的字典：\n",
    "# 实例\n",
    "# 创建三个字典，然后创建一个包含其他三个字典的字典：\n",
    "child1 = {\n",
    "  \"name\" : \"Phoebe Adele\",\n",
    "  \"year\" : 2002\n",
    "}\n",
    "child2 = {\n",
    "  \"name\" : \"Jennifer Katharine\",\n",
    "  \"year\" : 1996\n",
    "}\n",
    "child3 = {\n",
    "  \"name\" : \"Rory John\",\n",
    "  \"year\" : 1999\n",
    "}\n",
    "\n",
    "myfamily = {\n",
    "  \"child1\" : child1,\n",
    "  \"child2\" : child2,\n",
    "  \"child3\" : child3\n",
    "}\n",
    "\n",
    "print(myfamily)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "71dc5572",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'brand': 'Porsche', 'model': '911', 'year': 1963, 'color': 'White'}\n"
     ]
    }
   ],
   "source": [
    "# Python 字典 update() 方法\n",
    "# 向字典插入项目：\n",
    "thisdict = {\n",
    "  \"brand\": \"Porsche\",\n",
    "  \"model\": \"911\",\n",
    "  \"year\": 1963\n",
    "}\n",
    "\n",
    "thisdict.update({\"color\": \"White\"})\n",
    "\n",
    "print(thisdict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24f125f5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
